package cn.tedu.carehome.foundation.controller;


import cn.tedu.carehome.foundation.pojo.dto.FoodPackageAddNewDTO;
import cn.tedu.carehome.foundation.pojo.dto.FoodPackageLinkAddNewDTO;
import cn.tedu.carehome.foundation.pojo.dto.FoodPackageLinkUpdateDTO;
import cn.tedu.carehome.foundation.pojo.dto.FoodPackageUpdateDTO;
import cn.tedu.carehome.foundation.pojo.vo.FoodPackageAdminVO;
import cn.tedu.carehome.foundation.pojo.vo.FoodPackageLinkAdminVO;
import cn.tedu.carehome.foundation.pojo.vo.FoodPackageLinkUpdateVO;
import cn.tedu.carehome.foundation.pojo.vo.FoodPackageUpdateVO;
import cn.tedu.carehome.foundation.restful.JsonPage;
import cn.tedu.carehome.foundation.restful.WebConsts;
import cn.tedu.carehome.foundation.service.IFoodPackageLinkService;
import cn.tedu.carehome.foundation.service.IFoodPackageService;
import cn.tedu.carehome.foundation.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/food-package-links")
@Api(tags = "4.套餐餐品关联管理模块")
public class FoodPackageListController {

    @Autowired
    private IFoodPackageLinkService foodPackageLinkService;

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('food-package-link/add-new')")
    @ApiOperation("添加套餐餐品关联")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid FoodPackageLinkAddNewDTO foodPackageLinkAddNewDTO){
        log.debug("开始处理添加餐品套餐关联请求,参数:{}",foodPackageLinkAddNewDTO);
        foodPackageLinkService.addNew(foodPackageLinkAddNewDTO);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据id删除套餐餐品关联")
    @PreAuthorize("hasAuthority('food-package-link/delete')")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "关联物ID", required = true, example = "9527", dataType = "long")
    public JsonResult<Void> deleteById(@Range(min = 1, max = 10000, message = "删除关联失败，关联ID非法！")
                         @PathVariable Long id) {
        log.debug("开始【模拟】处理【根据id删除关联】的请求，参数：{}", id);
        foodPackageLinkService.deleteById(id);
        return JsonResult.ok();
    }


    @PostMapping("/update")
    @ApiOperation("修改关联")
    @PreAuthorize("hasAuthority('food-package-link/update')")
    @ApiOperationSupport(order = 300)
    public JsonResult<Void> update(@Valid FoodPackageLinkUpdateDTO foodPackageLinkUpdateDTO) {
        log.debug("开始处理修改关联请求,参数:{}",foodPackageLinkUpdateDTO);
        foodPackageLinkService.update(foodPackageLinkUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation(value="根据id请求关联信息")
    @PreAuthorize("hasAuthority('food-package-link/update')")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id")
    public JsonResult<FoodPackageLinkUpdateVO>  selectById(@Range(min = 1, max = 10000, message = "删除关联失败，关联ID非法！")
                                         @PathVariable Long id){
        log.debug("查询的id是:id={}",id);
        FoodPackageLinkUpdateVO foodPackageLinkUpdateVO=foodPackageLinkService.selectById(id);
        return JsonResult.ok(foodPackageLinkUpdateVO);
    }


    @GetMapping("/list")
    @ApiOperation(value="请求关联列表")
    @PreAuthorize("hasAuthority('food-package-link/read')")
    @ApiOperationSupport(order = 200)
    public JsonResult<List<FoodPackageLinkAdminVO>> select(){
        log.debug("开始处理后台请求餐品列表");
        List<FoodPackageLinkAdminVO> list=foodPackageLinkService.queryFoodPackageLinkList();
        return JsonResult.ok(list);
    }

    @GetMapping("/list-by-page")
    @ApiOperation(value="请求关联列表")
    @PreAuthorize("hasAuthority('food-package-link/read')")
    @ApiOperationSupport(order = 200)
    public JsonResult<JsonPage<FoodPackageLinkAdminVO>> selectByPage(
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE)
                    Integer page,
            @RequestParam(required = false,defaultValue = WebConsts.DEFAULT_PAGE_SIZE)
                    Integer pageSize

    ){
        log.debug("开始处理后台请求餐品列表");
        JsonPage<FoodPackageLinkAdminVO> jsonPage = foodPackageLinkService.queryFoodPackageLinkListByPage(page,pageSize);
        return JsonResult.ok(jsonPage);
    }
}
